#include <dspSimulator/instruction/ldh.h>

bool LDH::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void LDH::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                            std::shared_ptr<llvm::IRBuilder<>> irBuilder) {

}

std::shared_ptr<Instruction> LDH::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<LDH> ldh;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<5>(data, 2, 0b10001) && checkBits<1>(data, 8, 0b0) && existStandardCondition(data)) {
            ldh = std::make_shared<LDH>(dspInstInfo->getAddress(), baseCycle);
            ldh->creg_z = extractBits<4>(data, 28);
            ldh->dst = extractBits<5>(data, 23).to_ulong();
            ldh->baseR = extractBits<5>(data, 18).to_ulong();
            ldh->offsetR = extractBits<5>(data, 13).to_ulong();
            ldh->cst = ldh->offsetR;
            ldh->mode = extractBits<4>(data, 9).to_ulong();
            ldh->y = data[7];
            if (ldh->y == 0) {
                ldh->funcUnit = FuncUnit::D1;
            } else {
                ldh->funcUnit = FuncUnit::D2;
            }
            ldh->s = data[1];
            ldh->p = data[0];
            ldh->instFormat = InstFormat::Kind1;
        } else if (checkBits<5>(data, 2, 0b10011) && existStandardCondition(data)) {
            ldh = std::make_shared<LDH>(dspInstInfo->getAddress(), baseCycle);
            ldh->creg_z = extractBits<4>(data, 28);
            ldh->dst = extractBits<5>(data, 23).to_ulong();
            ldh->cst = extractBits<15>(data, 8).to_ulong();
            ldh->funcUnit = FuncUnit::D2;
            ldh->y = data[7];
            ldh->s = data[1];
            ldh->p = data[0];
            ldh->instFormat = InstFormat::Kind2;
        }
    }
    return ldh;
}

void LDH::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (executePhase == ExecutePhase::E1) {
        if (instFormat == InstFormat::Kind1) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            m_visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 1, dspMachine);
        } else if (instFormat == InstFormat::Kind2) {
            u32 *baseRPtr;
            if (y == 0) {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName("B14")->getDataPtr();
            } else {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName("B15")->getDataPtr();
            }
            m_visitMemAddr = *baseRPtr + (cst << 1);
        }
    } else if (executePhase == ExecutePhase::E3) {
        m_data = dspMachine->getMMU()->getMemory()->readNBytes(m_visitMemAddr, 2);
    } else if (executePhase == ExecutePhase::E5) {
        u16 num = vectorToBitSet<16>(m_data).to_ulong();
        *dstLoRegPtr = extractBitsAndToInt<16>(num);
    }
}

std::string LDH::toString() const {
    std::string res;
    if (isCompactInstruction) {

    } else {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    switch (instFormat) {
        case InstFormat::Kind1:
            res += addressModeToString() + "," + getDstReg32Name();
            break;
        case InstFormat::Kind2:
            if (y == 0) {
                res = res + " B14[" + std::to_string(cst) + "]," + getDstReg32Name();
            } else {
                res = res + " B15[" + std::to_string(cst) + "]," + getDstReg32Name();
            }
            break;
        default:
            break;
    }
    return res;
}

std::shared_ptr<DspInstruction> LDH::clone() {
    return std::make_shared<LDH>(*this);
}
